<?php

namespace app\admin\controller\questions;

use app\admin\controller\Controllers;
use app\admin\service\questions\QuestionCategory as QuestionCategoryService;
use app\admin\model\questions\QuestionCategory as QuestionCategoryModels;

class QuestionCategory extends Controllers
{
    /**
     * 显示资源列表
     * @param QuestionCategoryService $service
     *
     * @return \think\Response
     */
    public function index( QuestionCategoryService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\questions\QuestionCategory.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
        return $this->fetch('questions/question_category/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create( QuestionCategoryService $service )
    {
        $this->assign([
            'second' => $service->getSecond()
        ]);
        return $this->fetch('questions/question_category/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  QuestionCategoryService $service
     * @return \think\Response
     */
    public function save(QuestionCategoryService $service)
    {
        $post = $this->request->only(['title','pid'], 'post');
        $this->validate($post, 'app\admin\validate\questions\QuestionCategory.Create');
        $questionCategoryId = $service->create($post);
        return false === $questionCategoryId?failed('新增失败'):success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  QuestionCategoryModels $model
     * @param QuestionCategoryService $service
     * @return \think\Response
     */
    public function edit(QuestionCategoryModels $model, QuestionCategoryService $service)
    {
        $this->assign([
            'data' => $model,
            'second' => $service->getSecond()
        ]);
        return $this->fetch('questions/question_category/edit');
    }

    /**
     * 保存更新的资源
     * @param  QuestionCategoryModels $model
     * @param  QuestionCategoryService $service
     *
     * @return \think\Response
     */
    public function update(QuestionCategoryModels $model, QuestionCategoryService $service)
    {
        $put = $this->request->only(['title','pid'], 'put');
        $this->validate($put, 'app\admin\validate\questions\QuestionCategory.Update');
        $update = $service->save($model, $put);
        return false === $update?failed('编辑失败'):success();
    }

    /**
     * 删除指定资源
     *
     * @param QuestionCategoryService $service
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(QuestionCategoryService $service, int $id)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\questions\QuestionCategory.delete');
        return $service->destory($id)?success():failed('删除失败');
    }

    /**
     * 批量删除
     * @param QuestionCategoryService $service
     * @return \think\Response
     */
    public function destory(QuestionCategoryService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\questions\QuestionCategory.destory');
        return $service->destory($delete['ids'])?success():failed('删除失败');
    }

}
